package com.ruoyi.project.purchase.service.impl;

import com.ruoyi.project.purchase.domain.P_drug;
import com.ruoyi.project.purchase.domain.Purch_drug;
import com.ruoyi.project.purchase.domain.Purchase;
import com.ruoyi.project.purchase.domain.VO;
import com.ruoyi.project.purchase.mapper.P_DrugMapper;
import com.ruoyi.project.purchase.mapper.Purch_drugMapper;
import com.ruoyi.project.purchase.mapper.PurchaseMapper;
import com.ruoyi.project.purchase.service.Purch_drugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Service
@Transactional
public class Purch_drugServiceImpl implements Purch_drugService {
    @Autowired
    private Purch_drugMapper purch_drugMapper;
    @Autowired
    private P_DrugMapper p_drugMapper;
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Override
    public List<Purch_drug> findAllDrugByPurOrderId(String purOrderId) {
        return purch_drugMapper.findDrugByPurOrderId(purOrderId);
    }

    @Override
    public List<P_drug> findAllType() {
        return p_drugMapper.findType();
    }

    @Override
    public List<P_drug> findAllDrug(P_drug p_drug , Integer [] showedDrugIds) {
        List<P_drug> allDrugBy = p_drugMapper.findAllDrugBy(p_drug);
        for (int i = 0; i < allDrugBy.size(); i++) {
            for (int j = 0; j < showedDrugIds.length; j++) {
                if (allDrugBy.get(i).getDrugId()==showedDrugIds[j]){
                    allDrugBy.remove(i);
                }
            }
        }
        return allDrugBy;
    }

    @Override
    public int addDrugPur(VO vo) {
        Double num = 0.0;
        if (vo.getCreateTime()==null){
            vo.setCreateTime(new Date());
        }
        for (Purch_drug purch_drug: vo.getDrugList()) {
            String purOrderId = purch_drugMapper.getOnePurchaseDrug(vo, purch_drug);
            System.out.println(purOrderId+"1");
            System.out.println(purOrderId!=null);
            if (purOrderId!=null){
                num += purch_drug.getPurNumbers() * purch_drug.getPurPrice();
                int i = purch_drugMapper.addDrugNum(vo, purch_drug);
            }else {
                num += purch_drug.getPurNumbers() * purch_drug.getPurPrice();
                int i = purch_drugMapper.addDrugPur(vo, purch_drug);
            }
        }
        vo.setPurCount(num);
        int i=0;
        if (vo.getStatus()!=1) {
            String onePurchase = purchaseMapper.getOnePurchase(vo);
            System.out.println(onePurchase);
            System.out.println(onePurchase!=null);
            if (onePurchase==null){
                i = purchaseMapper.addPurchase(vo);
            }else {
                i = purchaseMapper.updatePurchase(vo);
            }
        }else {
            String onePurchase = purchaseMapper.getOnePurchase(vo);
            System.out.println(onePurchase);
            System.out.println(onePurchase!=null);
            if (onePurchase==null) {
                i = purchaseMapper.addPurchaseAndStatus(vo);
            }else {
                i = purchaseMapper.updatePurchaseAndStatus(vo);
            }
        }
        return i;
    }

    @Override
    public int deletePurDrug(Integer drugId, Purchase purchase) {
        return purch_drugMapper.deletePurDrug(drugId, purchase);
    }
}
